<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class getsite extends CI_Controller {
	var $cookieFileName;
	function __construct() {
		
        parent::__construct();
		$this->load->library('get_data_html');
		$this->load->model("msoft");
		$this->cookieFileName = dirname (realpath(__FILE__)).'/'.$this->cookieFileName; 
    }
	
	function index(){
	
	}
	
	function teststring(){
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
';
	$str = "Phần mềm kh&#225;c";
	//$str = mb_convert_encoding($str, "UTF-8");
	//$str = iconv('auto', 'utf-8', $str);
	foreach(mb_list_encodings() as $chr){ 
        echo mb_convert_encoding($str, 'UTF-8', 'auto')." : ".$chr."<br>";    
 } 
 //echo mb_convert_encoding($str, 'UTF-8', 'auto')."<br>"; 
	
	
	}
	
	function category(){
	$this->load->model("mcategory");
	$cates = $this->mcategory->gets(array('GET_STATUS'=>0, 'CAT_ID >'=>248));
	foreach($cates as $cate){
		if($cate->GET_LINK == NULL || $cate->GET_LINK == '')
			continue;
		$arr_insert = array();
		$html_content = $this->get_data_html->get_content($cate->GET_LINK);
		
		$array = array('type' => '1',
			'estart' => '<ul id="leftnavigation" class="leftnavigation">',
			'eend' => '<\/div>',
		);
		if($cate->CAT_PARENT >0){
			$array = array('type' => '1',
				'estart' => '<div class="item-title">',
				'eend' => '<\/div>',
			);
		}
		$arr_cates = $this->get_data_html->load($html_content, $array);
		if($cate->CAT_PARENT ==0){
			$arr_cates = $this->get_data_html->removetag('<ul class=\'clearfix\'>', '<\/ul>',$arr_cates[0]);
			$arr_cates = $this->get_data_html->removetag('<a class="first">', '<\/a>',$arr_cates);
			
			$array = array('type' => '1',
			'estart' => '<li>',
			'eend' => '<\/li>',
			);
			$arr_cates = $this->get_data_html->load($arr_cates, $array);
		}

		//$arr_cates = strip_tags($arr_cates,'<a><img>');
		
 
		 
		for($i=0; $i<count($arr_cates); $i++){
			$scate = $arr_cates[$i];
			if($scate == '')
				continue;
			$arr_insert = array();
			$link = $this->get_data_html->GetBetween($scate,'href="', '"');
			$link = "http://www.download.com.vn".strip_tags(trim($link));
				
			$check = $this->mcategory->get($link,'GET_LINK');
		
			$name = $this->get_data_html->GetBetween($scate,'">', '</a');
			$name = strip_tags(trim($name));
				 
			if($check || $link =='' || $name == '' || $name == 'Danh mục'){
		
				continue;
			}
			
			$thumb = $this->get_data_html->GetBetween($scate,'<img src="', '"');
			$thumb = strip_tags(trim($thumb));
		
		
		$tags = get_meta_tags($link);
		$arr_insert['SEO_KEYWORD'] = trim($tags['keywords']);
		$arr_insert['SEO_DESCRIPTION'] = ($tags['description']);
		$arr_insert['CAT_NAME'] = $this->seo->DecimalToUni(trim($name));
		
		$arr_insert['CAT_CODE'] = $this->seo->seolink($arr_insert['CAT_NAME']);
		
		
			$catcode = $this->getRootParent($cate->CAT_ID);
				$arr_insert['CAT_CODE'] = $catcode."-".$arr_insert['CAT_CODE'];
		
			$arr_insert['CAT_CODE'] = strtolower($arr_insert['CAT_CODE']);
			
		$ttid[] =$cate->CAT_ID;
		$arr_insert['CAT_PARENT'] =$cate->CAT_ID;
		$arr_insert['GET_LINK'] =$link;
		$arr_insert['CAT_THUMB'] =$thumb;
		//$this->mcategory->insert($arr_insert);
		echo $arr_insert['CAT_NAME']."<br>";
	
		}

		//$this->mcategory->update($cate->CAT_ID,"CAT_ID",array('GET_STATUS'=>1));
		
	}
	
	}
	function getRootParent($parentID){
		$tcate = $this->mcategory->get($parentID,'CAT_ID');
		if($tcate && $tcate->CAT_PARENT ==0){
			return $tcate->CAT_CODE;
		}
		 return $this->getRootParent($tcate->CAT_PARENT);
	}
	
	function soft($cateid = 5, $page = 1){
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
		$this->load->model("mcategory");
		$cates = $this->mcategory->gets(array('CAT_ID >'=>5, 'CAT_ID'=>$cateid ));
		if($cateid < 253|| $cateid >374)
			return '';
		foreach($cates as $cate){
			$html_content = $this->get_data_html->get_content($cate->GET_LINK);
		
			$array = array('type' => '1',
				'estart' => '<li class="PagedList-skipToLast">',
				'eend' => '<\/li>',
			);
			$maxPage = $this->get_data_html->load($html_content, $array);
			if(count($maxPage) >0)
				$maxPage = $maxPage[0];
			else
				$maxPage= 1;
			$maxPage = strip_tags($maxPage);
			
			$nextCate = $cateid;
			$nextPage = $page;
			
			if($page == 0) {
				 $nextCate++;
				 $this->db->where("CONF_CODE",'GET_SOFT_CATID');
				$this->db->update("config",array('CONF_VALUE'=>$cateid));

			}
			if($page ==-1){
				$nextPage = $maxPage;
			}
			else {
				$nextPage = $nextPage -1;
			}
	echo '<meta http-equiv="refresh" content="1;URL=\''.base_url().'getsite/soft/'.$nextCate.'/'.$nextPage.'\'" />';
	
	if($page < 1) 
		return '';
				if(strpos($cate->GET_LINK, '.aspx'))
					$link = str_replace("index.aspx","index".$page.".aspx",$cate->GET_LINK);
				else
				$link = $cate->GET_LINK.'?p='.$page;
				echo 'Get URL: '.$link.'<br>';
				$html_content = $this->get_data_html->get_content($link);
				
				$array = array('type' => '1',
				'estart' => '<div class="list-item clearfix">',
				'eend' => '<\/h2>',
			);
			$softs = $this->get_data_html->load($html_content, $array);
				for($j = count($softs) -1; $j>=0; $j--){
					$soft = $softs[$j];
						
					$link = $this->get_data_html->GetBetween($soft,'href="', '"');
					$link = "http://www.download.com.vn".strip_tags(trim($link));
					
					$name = $this->get_data_html->GetBetween($soft,'<b>', '</b>');
					$name = strip_tags(trim($name));
					
					$this->db->where("GET_LINK", $link);
					$check = $this->db->get("getsoft")->result();
					if($check || $link =='' || $name ==''){
						echo '<font color="#F00">'.$name."</font><br>";
						continue;
					}
					$arr_insert = array();
					$arr_insert['GET_LINK'] = $link;
					$arr_insert['SOFT_NAME'] = $name;
					$this->db->insert("getsoft",$arr_insert);
					echo '<font color="#00f">'.$j.'-'.$name."</font><br>";
					}
		
		}
		
	
	}
	
	function parse_detail($link = ''){
	$link = 'E:\file\1379.html';
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	$html_content = $this->get_data_html->get_content($link);
	
	$man = $this->get_data_html->GetBetween($html_content, '<div class="publisher-softwares">','</div>');	
	$man = $this->get_data_html->GetBetween($man, 'href="','"');	
	$man = str_replace('Phát hành:','',$man);
	$man = trim(strip_tags($man));
	
	
	$lic = $this->get_data_html->GetBetween($html_content, '<li class="license-info clearfix">','</li>');	
	$lic = str_replace(array('<span class="item-info">','<span class="item-label">'),'',$lic);
	$lic = $this->get_data_html->GetBetween($lic, '<span class="','"');
	$lic = trim(strip_tags($lic));
	
	$array = array('type' => '1',
				'estart' => '<span typeof="v:Breadcrumb">',
				'eend' => '<\/span>',
			);
	$cate = $this->get_data_html->load($html_content, $array);
	//$cate = $this->get_data_html->GetBetween($html_content, '<span typeof="v:Breadcrumb">','</span>');
	$cate = end($cate);
	$cate = $this->get_data_html->GetBetween($cate, 'href="','"');
	$cate = str_replace(array('Dung lượng:','<span class="item-label">'),'',$cate);
	$cate = trim(strip_tags($cate));
	$cate = 'http://www.download.com.vn'.$cate;
	
	
	
	
	$name_desc = $this->get_data_html->GetBetween($html_content, '<h1 class="detail-title">','</h1>');
	$name_desc = $this->get_data_html->GetBetween($name_desc, '<i>','</i>');
	$name_desc = trim(strip_tags($name_desc));
	
	
	$detail = $this->get_data_html->GetBetween($html_content, '<div class="content-overview">','<div class="clear"></div>');
	$detail = $this->get_data_html->removetag('<h3 class="title"><span id="IntroLabel">', '<\/h3>',$detail);
	$detail = $this->get_data_html->removetag('<div', '>',$detail);
	$detail = str_replace('</div>','',$detail);
	$detail = trim($detail);
	
	
	$size = $this->get_data_html->GetBetween($html_content, '<li class="filesize-info">','</li>');
	$size = str_replace(array('Dung lượng:','<span class="item-label">'),'',$size);
	$size = trim(strip_tags($size));
	
	$time_create = $this->get_data_html->GetBetween($html_content, '<li class="dateadded-info clearfix">','</li>');
	$time_create = $this->get_data_html->GetBetween($time_create, '<span class="item-info">','</span>');
	$time_create = trim(strip_tags($time_create));
	$time_create = explode("/",$time_create);
	$time_create = mktime(0, 0, 0, $time_create[1], $time_create[0], $time_create[2]);

	
	$time_update = $this->get_data_html->GetBetween($html_content, '<span class="item-label">Ngày cập nhật:','</li>');
	$time_update = $this->get_data_html->GetBetween($time_update, '<span class="item-info">','</span>');
	$time_update = trim(strip_tags($time_update));
	$time_update = explode("/",$time_update);
	$time_update = mktime(0, 0, 0, $time_update[1], $time_update[0], $time_update[2]);
	
	
	$thumb = $this->get_data_html->GetBetween($html_content, '<div class="screenshots">','</a>');
	$thumb = $this->get_data_html->GetBetween($thumb, 'src="','"');
	$thumb = trim(strip_tags($thumb));

	
	$icon = $this->get_data_html->GetBetween($html_content, '<h1 class="detail-title">','/>');
	$icon = $this->get_data_html->GetBetween($icon, 'src="','"');
	$icon = trim(strip_tags($icon));
	
	
	$version = $this->get_data_html->GetBetween($html_content, '<h1 class="detail-title">','</h1>');
	$version = $this->get_data_html->GetBetween($version, '<em>','</em>');
	$version = trim(strip_tags($version));
	
	
	$numdownload = $this->get_data_html->GetBetween($html_content, '<li class="downloads-info clearfix">','</li>');
	$numdownload = $this->get_data_html->GetBetween($numdownload, '<span class="item-info">','</span>');
	$numdownload = trim(strip_tags($numdownload));
	$numdownload = str_replace(array('.',','),'',$numdownload);
	
	
	$os = $this->get_data_html->GetBetween($html_content, '<li class="requirements-info clearfix">','</li>');
	$os = $this->get_data_html->GetBetween($os, '<span class="item-info">','</span>');
	$os = trim(strip_tags($os));
	
	
	$tags = get_meta_tags($link);
	
	
	$arr_insert['SGET_DATA'] = array();
	$arr_insert['SGET_DATA']['MAN_ID'] = 'http://www.download.com.vn'.$man;
	$arr_insert['SGET_DATA']['LIC_ID'] = $lic;
	$arr_insert['SGET_DATA']['CAT_ID'] = $cate;
	$arr_insert['SOFT_NAME_DESCRIPTION'] =$this->seo->DecimalToUni($name_desc);
	$arr_insert['SOFT_CONTENT'] = $this->seo->encode_tag_html($this->seo->DecimalToUni($detail));
	$arr_insert['SOFT_CONTENT_SHORT'] = null;
	$arr_insert['SOFT_FILESIZE'] = $size;
	//$arr_insert['SOFT_CODE'] = $this->seo->seolink();
	$arr_insert['SOFT_TIME_CREATE'] = $time_create;
	$arr_insert['SOFT_TIME_UPDATE'] = $time_update;
	$arr_insert['SOFT_THUMB'] = $thumb;
	$arr_insert['SOFT_ICON'] = $icon;
	$arr_insert['SOFT_VERSION'] = $version;
	$arr_insert['SOFT_DOWN'] = $numdownload;
	$arr_insert['SOFT_STATUS'] = 1;
	$arr_insert['SOFT_TIME_USE_LIMIT'] = null;
	$arr_insert['SOFT_REQUIREMENT_OS'] = $os;
	$arr_insert['SEO_KEYWORD'] = trim($tags['keywords']);
	$arr_insert['SEO_DESCRIPTION'] = trim($tags['description']);
	$arr_insert['GET_DATA'] = json_encode($arr_insert['SGET_DATA']);
	unset($arr_insert['SGET_DATA']);
	return $arr_insert;

	}
	
	function get_detail(){
	$this->db->where("GET_STATUS",1);
	$softs = $this->db->get("getsoft")->result();
	foreach ($softs as $soft){
	$link = 'E:\file\\'.$soft->GET_ID.'.html';
	$arr_insert = array();
	$arr_insert = $this->parse_detail($link);
	$arr_insert['GET_STATUS'] = 2;
	$this->db->where("GET_ID",$soft->GET_ID);
	$this->db->update("getsoft",$arr_insert);
	echo $soft->SOFT_NAME."<br>";
	//echo $this->db->last_query();
	 
	}

	}
	
	function trans($getID = 0){
		$this->load->model("msoft");
		$this->db->where("GET_ID",$getID);
		$soft = $this->db->get("getsoft")->first_row();
		$check = $this->msoft->get($soft->SOFT_NAME,'SOFT_NAME');
		if(!$check){
			$soft->SOFT_NAME = $this->seo->DecimalToUni($soft->SOFT_NAME);
			$soft->SOFT_CODE = $this->seo->seolink($soft->SOFT_NAME);
			echo "New id: ".$this->msoft->insert((array)$soft);
		
		}
		else {
			echo "Ton tai.";
		}
	}
}
?>